package com.zhongyu.user.consumer.controller;

import com.netflix.ribbon.proxy.annotation.Http;
import com.zhongyu.comm.util.AuthException;
import com.zhongyu.comm.util.ErrorCode;
import com.zhongyu.comm.vo.Result;
import com.zhongyu.user.consumer.service.UserService;
import com.zhongyu.user.consumer.vo.UserDto;
import com.zhongyu.user.consumer.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/p/login")
    public Result login(@RequestBody UserDto userDto){
            Object[] data=new Object[0];
            try {
                data=userService.searchUserVoAndToken(userDto.getPhone(),userDto.getPassword());
                return Result.success(data, ErrorCode.LoGIN_SUCCESS);
            }catch (AuthException ex){
               log.error(ex.getMessage(),ex);
                return Result.failure(ex.getMessage(),ErrorCode.LOGIN_ERROR);
            }
    }

    @PostMapping("/p/user/loadCurrentUserByToken")
    public Result loadCurrentUserByToken(@RequestHeader("token") String token){
      try {
          UserVo userVo=userService.searchUserByToken(token);
          return Result.success(userVo,ErrorCode.LoGIN_SUCCESS);
      }catch (Exception ex){
        return Result.failure("用户未登录或登录失效",ErrorCode.NO_LOGIN);
      }
    }



}
/*
@RequestParam
@RequestBody
@PathVariable
@RequestHeader
@CookieValue*/
